package com.neiquan.backstage.resourec_manage.imp;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.pub.bsc.dao.po.Entrepreneur;
import com.neiquan.backstage.resourec_manage.EntrepreneurService;

@Service
public class EntrepreneurServiceImp implements EntrepreneurService {
	private static Class<?> poc = Entrepreneur.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;

	@Override
	public Entrepreneur queryByEntrepreneurUnique(String entrepreneurUnique) {
		// TODO Auto-generated method stub
		return extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "uniqueId", entrepreneurUnique);
	}

	@Override
	public void add(Entrepreneur entity) {
		// TODO Auto-generated method stub
		extraSpringHibernateTemplate.getHibernateTemplate().save(entity);
	}

	@Override
	public Entrepreneur queryById(int id) {
		// TODO Auto-generated method stub
		return extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "entrepreneurId", id);
	}

	@Override
	public void updata(Entrepreneur entity) {
		// TODO Auto-generated method stub
		extraSpringHibernateTemplate.getHibernateTemplate().update(entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Entrepreneur> queryAll() {
		// TODO Auto-generated method stub
		return (List<Entrepreneur>) extraSpringHibernateTemplate.findAll(poc);
	}

	@Override
	public Entrepreneur queryByName(String name, Integer memberId) {
		List<Entrepreneur> entrepreneurs = (List<Entrepreneur>) extraSpringHibernateTemplate.getHibernateTemplate().find("From Entrepreneur where name=? and memberId=?", name, memberId);
		if (entrepreneurs.size() > 0) {
			return entrepreneurs.get(0);
		}
		return null;
	}

	@Override
	public List<Entrepreneur> queryAll(int memberId) {
		// TODO Auto-generated method stub
		List<Entrepreneur> entrepreneurs = extraSpringHibernateTemplate.findAllByPropEq(poc, "memberId", memberId);
		return entrepreneurs;
	}

	@Override
	public List<Entrepreneur> queryAllChecked(int memberId) {
		DetachedCriteria detached = DetachedCriteria.forClass(poc);
		detached.add(Restrictions.eq("memberId", memberId));
		detached.add(Restrictions.eq("status", 1));
		detached.addOrder(Order.asc("entrepreneurId"));
		return extraSpringHibernateTemplate.findAllByCriteria(detached);
	}

	@Override
	public Entrepreneur queryByUniqueId(String uniqueId, Integer memberId) {
		// TODO Auto-generated method stub
		List<Entrepreneur> find = (List<Entrepreneur>) extraSpringHibernateTemplate.getHibernateTemplate().find("From Entrepreneur where uniqueId=? and memberId=?", uniqueId, memberId);
		if (find.size() > 0) {
			return find.get(0);
		}
		return null;
	}
}
